// 未获取token跳转的授权页面
const loginPage = '/pages/login/index'
const baseUrl = 'http://chunfengai.top:28081'
const log = require('../utils/log')
    // const baseUrl = 'http://127.0.0.1:8080'
const request = (method, url, data, header) => {
    let options = {}
    return new Promise((resolve, reject) => {
        const wxreq = () => {
            options.url = `${baseUrl}` + url
            options.timeout = 100000
            options.method = method
            options.header = {
                Authorization: `Bearer ${wx.getStorageSync('token')}`,
                ...header,
            }
            options.data = data
            wx.request({
                ...options,
                success: function(res) {
                    if (res.data.code == 200) {
                        resolve(res.data)
                    } else if (res.data.code == 401) {
                        let code = wx.getStorageSync('jsCode')
                        if (!code) {
                            wx.navigateTo({
                                url: '/pages/login/index',
                            })
                        } else {
                            // token过期，重新请求token
                            wx.removeStorageSync('token')
                            wx.removeStorageSync('userInfo')
                            wx.login({
                                success: (res) => {
                                    console.log('wxlogin res :>> ', res)
                                    wx.setStorageSync('jsCode', res.code)
                                    request('post', '/authorization/wx/registerOrLogin', {
                                            jsCode: res.code,
                                            nickName: '测试',
                                        })
                                        .then((logginRes) => {
                                            const { data } = logginRes
                                            const { token, userInfo } = data
                                            console.log(userInfo)
                                            wx.setStorageSync('token', token)
                                            try {
                                                wx.setStorageSync('userInfo', JSON.stringify(userInfo))
                                            } catch (error) {
                                                console.log('error', error)
                                            }
                                            wxreq()
                                        })
                                        .catch((err) => {
                                            log.error('request /authorization/wx/registerOrLogin error :>>', options, err)
                                        })
                                },
                            })
                        }
                    } else {
                        log.error('request error :>>', options, res)
                        reject(res.data)
                    }
                },
                fail: function(err) {
                    log.error('request wx.request error :>>', options, err)
                    reject(err)
                },
            })
        }
        wxreq()
    })
}
exports.request = request